
import { getToken, setToken, removeToken, getTokenKey, setTokenKey, removeTokenKey, removeDefaultPath, setRefreshToken, removeRefreshToken } from '@/utils/auth'
import {logout,getInfo} from '@/api/modules/user.js'
import router, { resetRouter } from '@/router'
const state = {
  token: getToken(),
  tokenKey: getTokenKey(),
  // 当前用户拥有的角色列表
  roleList: [],
  // 当前用户正在使用的角色
  role: '',
  // 当前商业公司信息
  curBusiness: {},
  // 用户信息
  info: {},
}

const mutations = {
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_TOKEN_KEY: (state, tokenKey) => {
    state.tokenKey = tokenKey
  },
  SET_INFO_SIGN:(state, info) => {
    state.info.user.signPath = info
  },
  //用户头像
  SET_INFO_avatar:(state, info) => {
    state.info.user.avatar = info
  },
  SET_INFO: (state, info) => {
    state.info = info;
  },
  UPDATE_ROLE: (state, role) => {
    state.role = role
  },
  SET_ROUTER_POSITION: (state, routerPosition) => {
    state.routerPosition = routerPosition
  }
}

const actions = {

  // 账号密码登录
  login({ commit }, token) {
	  console.log(token)
    return new Promise((resolve, reject) => {
        commit('SET_TOKEN', token)
        setToken(token)
    })
  },

  // 通过tokenKey获取token
  getTokenByTokenKey({ commit }, tokenKey) {
    return new Promise((resolve, reject) => {
      getTokenByTokenKey(tokenKey).then(response => {
        const { body } = response
        if(body) {
          commit('SET_TOKEN', body.token)
          commit('SET_TOKEN_KEY', body.tokenKey)
          setToken(body.token)
          setTokenKey(body.tokenKey)
          setRefreshToken(body.refreshToken)
        }
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // 获取验证码
  getVerificationCode({ commit }, phone) {
    return new Promise((resolve, reject) => {
      getVerificationCode(phone).then(response => {
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // 查询手机号是否已注册
  validatePhone({ commit }, phone) {
    return new Promise((resolve, reject) => {
      validatePhone(phone).then(response => {
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // 用户注册
  register({ commit }, userInfo) {
    return new Promise((resolve, reject) => {
      register(userInfo).then(response => {
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // 获取用户信息
  getInfo({ commit }) {
    return new Promise((resolve, reject) => {
		getInfo().then(response => {
			const { user,roles } = response
			 commit('SET_INFO', response);
			 commit('UPDATE_ROLE', roles)
			 resolve(response)
		})
    })
  },
  // 修改个人签名
  setSignPath({ commit },imagePath) {
    return new Promise((resolve, reject) => {
		commit('SET_INFO_SIGN', imagePath);
		resolve()
    })
  },
   // 修改用户头像
   setAvatar({ commit },imagePath) {
    return new Promise((resolve, reject) => {
		commit('SET_INFO_avatar', imagePath);
		resolve()
    })
  },
  // 用户登出（前端登出）
  logout({ commit, state, dispatch }) {
    return new Promise((resolve, reject) => {
      logout().then(response => {
        removeToken()
        removeTokenKey()
        removeRefreshToken()
        removeDefaultPath()
        resetRouter()
        resolve()
      })
    })
  },

  // 删除令牌
  resetToken({ commit }) {
    return new Promise(resolve => {
      commit('SET_TOKEN', '')
      commit('SET_INFO', {})
      removeToken()
      removeTokenKey()
      removeRefreshToken()
      removeDefaultPath()
      commit('UPDATE_IS_INVALID', true)
      resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
